//
//  CategoryDetailViewController.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/22.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "CategoryDetailViewController.h"

#import "CategoryDetailCollectionViewCell.h"//商品CollectionViewCell

#import "GoodsDetailViewController.h"//商品详情页Controller

#import "AllCategroyView.h"//筛选View

#import "GoodsModel.h"//商品列表Model

@interface CategoryDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIImageView *priceArrow;//价格箭头
@property (weak, nonatomic) IBOutlet UILabel *priceeee;

@end

@implementation CategoryDetailViewController
{
    UICollectionView *_goodsCollectionView;
    NSMutableArray *_goodsDataArray;
    
    BOOL _isDescending;//价格排序,默认降序
    
    AllCategroyView *_filterView;//筛选View
    UIButton *_filterbgView;//筛选背景
}

 static NSString *const CategoryDetailCollectionViewCellID = @"CategoryDetailCollectionViewCell";



- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
    if (_searchArray) {
        if (!_goodsDataArray) {
            _goodsDataArray = [NSMutableArray array];
        }else{
            [_goodsDataArray removeAllObjects];
        }
        [_goodsCollectionView reloadData];
    }else{
        //网络请求
        [self requestCategoryDetail];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _isDescending = YES;
    
    //创建CollectionView
    [self createCollectionView];
  
}
- (void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _goodsCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 99, SCREEN_WIDTH, SCREEN_HEIGHT - 99) collectionViewLayout:layout];
    _goodsCollectionView.delegate = self;
    _goodsCollectionView.dataSource = self;
    _goodsCollectionView.showsVerticalScrollIndicator = NO;
    _goodsCollectionView.backgroundColor = BackgroundCOLOR;
    [self.view addSubview:_goodsCollectionView];
    
    [_goodsCollectionView registerNib:[UINib nibWithNibName:@"CategoryDetailCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CategoryDetailCollectionViewCellID];
}


#pragma mark === collectionViewDelegate ===
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _goodsDataArray.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCREEN_WIDTH - 30) / 2, (SCREEN_WIDTH - 30) / 2 + 60);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 0, 10);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CategoryDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CategoryDetailCollectionViewCellID forIndexPath:indexPath];
    GoodsModel *goods  = [_goodsDataArray objectAtIndex:indexPath.row];
    
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:goods.ProductSmallUrl]];
    cell.title.text = goods.Title;
    cell.price.text = goods.Price;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{


    GoodsModel *goods   = [_goodsDataArray objectAtIndex:indexPath.row];

    GoodsDetailViewController *goodsDetail = [[GoodsDetailViewController alloc] init];
    goodsDetail.modelId = goods.Id;
    goodsDetail.modelProductCode = goods.ProductCode;
    [self.navigationController pushViewController:goodsDetail animated:YES];
}



//上新
- (IBAction)new:(id)sender {
    NSLog(@"上新");
    [_goodsCollectionView reloadData];
}
//热销
- (IBAction)hot:(id)sender {
    NSLog(@"热销");
    [_goodsCollectionView reloadData];
}
- (IBAction)price:(id)sender {
    _isDescending = !_isDescending;
    NSLog(@"价格");
    if (_isDescending) {
        NSLog(@"降序");
    }else{
        NSLog(@"升序");
    }
    [_goodsCollectionView reloadData];
}
- (IBAction)filter:(id)sender {
    if (!_filterbgView) {
        _filterbgView = [UIButton buttonWithType:UIButtonTypeCustom];
        _filterbgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        _filterbgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        [_filterbgView addTarget:self action:@selector(hiddenFilterView) forControlEvents:UIControlEventTouchUpInside];
        [[UIApplication sharedApplication].keyWindow addSubview:_filterbgView];
    } 
    if (!_filterView) {
        _filterView = [[AllCategroyView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH - 50, SCREEN_HEIGHT)];
        [_filterbgView addSubview:_filterView];
    }
    
    _filterbgView.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        _filterView.frame = CGRectMake(50, 0, SCREEN_WIDTH - 50, SCREEN_HEIGHT);
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    }];
}

#pragma mark === 收起筛选 ===
- (void)hiddenFilterView{
    [UIView animateWithDuration:0.3 animations:^{
        _filterView.frame = CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH - 50, SCREEN_HEIGHT);
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    } completion:^(BOOL finished) {
        _filterbgView.hidden = YES;
    }];
}

#pragma mark === 网络请求 ===
- (void)requestCategoryDetail{
    NSDictionary *paraments;
    if (_index) {
        paraments = @{
                      @"productCat"      : _index,
                      @"brand"           : @"0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,424,425,426,427,428,429",
                      @"currentpage"     : @"1",
                      @"pagesize"        : @"40",
                      
                      @"query"           : @"",
                      @"pricelow"        : @"0",
                      @"pricehigh"       : @"0",
                      @"marketPriceLow"  : @"0",
                      @"marketPriceHigh" : @"0",
                      
                      @"sortfield"       : @"all",
                      @"desc"            : @"true",
                      @"pw"              : @"-1"
                      };
    }else{
        return;
    }
        
    [NetWorkManager requestWithType:HttpRequestTypeGet withMethodName:@"12082/api/BOMProductExtend/SearchPorductByCondition?" withParaments:paraments withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"-分类详情---- %@", object);
        if (isKindOfNSDictionary(object[@"Rows"])) {
            if (!_goodsDataArray) {
                _goodsDataArray = [NSMutableArray array];
            }else{
                [_goodsDataArray removeAllObjects];
            }
            
            NSDictionary *dict = object[@"Rows"];
            if (isKindOfNSArray([dict objectForKey:@"product"])) {
                NSArray *array = [dict objectForKey:@"product"];
                if (array.count)
                {
                    for (NSDictionary *smDic in array) {
                        GoodsModel *goods  = [GoodsModel parseUserWithDictionary:smDic];
                        
                        [_goodsDataArray addObject:goods];
                    }
                    [_goodsCollectionView reloadData];
                }
            }
        }
    } withFailureBlock:^(NSError *error) {
        NSLog(@"商品搜索网络请求失败 : %@", error);
    } progress:nil];

}
@end
